package com.xuan.friend.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuan.common.core.constants.CacheConstants;
import com.xuan.common.core.constants.Constants;
import com.xuan.common.core.utils.ThreadLocalUtils;
import com.xuan.common.redis.service.RedisService;
import com.xuan.friend.domain.user.User;
import com.xuan.friend.domain.user.vo.UserDetailVo;
import com.xuan.friend.mapper.user.UserMapper;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


@Component
public class UserCacheManager {


    @Resource
    private RedisService redisService;

    @Resource
    private UserMapper userMapper;

    @Value("${file.pathPrefix:}")
    private String pathPrefix;


    // ----------------------------用户详情缓存--------------------------------
    public UserDetailVo getUserById(Long userId) {
        String userDetailKey = getUserDetailKey(userId);
        UserDetailVo userDetailVo = redisService.getCacheObject(userDetailKey, UserDetailVo.class);
        // 1. 从 redis 中查到缓存,延期并返回
        if (userDetailVo != null) {
            redisService.expire(userDetailKey, CacheConstants.USER_EXP, TimeUnit.MINUTES);
            return userDetailVo;
        }
        // 2. 从数据库中查询
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .select(
                        User::getUserId,
                        User::getPhone,
                        User::getEmail,
                        User::getNickName,
                        User::getHeadImage,
                        User::getIntroduce,
                        User::getSex,
                        User::getMajorName,
                        User::getSchoolName,
                        User::getWechat,
                        // 这个字段用于限制被拉黑用户的操作
                        User::getStatus
                )
                .eq(User::getUserId, userId));
        if (user == null) {
            return null;
        }


        refreshUser(user);
        userDetailVo = new UserDetailVo();
        user.setHeadImage(pathPrefix + user.getHeadImage());
        BeanUtils.copyProperties(user, userDetailVo);
        return userDetailVo;
    }

    public void refreshUser(User user) {
        UserDetailVo userDetailVo = new UserDetailVo();
        BeanUtils.copyProperties(user, userDetailVo);
        String headImage = user.getHeadImage();
        if (StringUtils.isNotBlank(headImage)) {
            userDetailVo.setHeadImage(pathPrefix + headImage);
        }
        // 刷新用户详情缓存,设置 10 分钟过期时间
        String userDetailKey = getUserDetailKey(user.getUserId());
        redisService.setCacheObject(userDetailKey, userDetailVo, CacheConstants.USER_EXP, TimeUnit.MINUTES);
    }

    public void removeUserCache() {
        Long userId = ThreadLocalUtils.get(Constants.USER_ID, Long.class);
        String userDetailKey = getUserDetailKey(userId);
        redisService.deleteObject(userDetailKey);
    }




    private String getUserDetailKey(Long userId) {
        return CacheConstants.USER_DETAIL_KEY + userId;
    }

}
